﻿namespace REVISIONMAX20
{
    using System;
    using System.Data.SQLite;
    using System.Text;

    public class SQLItem : ObjectBase
    {
        private int _connectionstringid;
        private DateTime? _executed;
        private DateTime? _expired;
        private int _index;
        private int _revision;
        private string _source;
        private string _sourcetype;

        public SQLItem(SQLiteDataReader reader, ObjectBase owner) : base("sqlitems", reader, owner)
        {
            this._connectionstringid = 0;
            this._index = reader.GetInt32(4);
            this._revision = reader.GetValue(5).IntValueOrZero();
            this._source = reader.GetValue(6).StringValueOrEmpty();
            this._sourcetype = reader.GetValue(7).StringValueOrEmpty();
            this._connectionstringid = reader.GetInt32(8);
            this._executed = reader.GetValue(9).DateValueOrNull();
            this._expired = reader.GetValue(10).DateValueOrNull();
        }

        public SQLItem(string psource, string psourcetype, ObjectBase owner, SQLiteConnection con) : base("sqlitems", owner, con)
        {
            this._connectionstringid = 0;
            this._source = psource;
            this._sourcetype = psourcetype;
        }

        public StringBuilder SaveOrUpdate()
        {
            StringBuilder errors = new StringBuilder();
            try
            {
                using (SQLiteConnection connection = Common.GetSQLiteConnection())
                {
                    connection.Open();
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
                    {
                        using (SQLiteCommand command = connection.CreateSQLItemCommand())
                        {
                            this.SaveOrUpdate(command, errors);
                        }
                        if (errors.Length == 0)
                        {
                            transaction.Commit();
                            return errors;
                        }
                        transaction.Rollback();
                        return errors;
                    }
                }
            }
            catch (Exception exception)
            {
                base.Rollback();
                errors.AppendLine(string.Format("Error: {0}", exception.Message));
            }
            return errors;
        }

        public void SaveOrUpdate(SQLiteCommand command, StringBuilder errors)
        {
            if (base.Status != Status.StatusPopulated)
            {
                try
                {
                    base.UpdateCommand(command);
                    command.Parameters["@index"].Value = this._index;
                    command.Parameters["@revision"].Value = this._revision.ToString();
                    command.Parameters["@source"].Value = this._source;
                    command.Parameters["@sourcetype"].Value = this._sourcetype;
                    command.Parameters["@connectionstringid"].Value = this._connectionstringid;
                    command.Parameters["@executed"].Value = this._executed;
                    command.Parameters["@expired"].Value = this._expired;
                    if (command.ExecuteNonQuery() <= 0)
                    {
                        errors.AppendLine(string.Format("Error: {0} {1} {2} failed.", base.tablename, base.id, (base.Status == Status.StatusNew) ? "insert" : "update"));
                    }
                    else
                    {
                        base.Commit();
                        base.Persist();
                    }
                }
                catch (Exception exception)
                {
                    errors.AppendLine(string.Format("Error: {0}", exception.Message));
                }
            }
        }

        public int connectionstringid
        {
            get
            {
                return this._connectionstringid;
            }
            set
            {
                base.IsDirty = value != this._connectionstringid;
                this._connectionstringid = value;
            }
        }

        public DateTime? executed
        {
            get
            {
                return this._executed;
            }
            set
            {
                base.IsDirty = value != this._executed;
                this._executed = value;
            }
        }

        public DateTime? expired
        {
            get
            {
                return this._expired;
            }
            set
            {
                base.IsDirty = value != this._expired;
                this._expired = value;
            }
        }

        public int index
        {
            get
            {
                return this._index;
            }
            set
            {
                base.IsDirty = value != this._index;
                this._index = value;
            }
        }

        public int revision
        {
            get
            {
                return this._revision;
            }
            set
            {
                base.IsDirty = value != this._revision;
                this._revision = value;
            }
        }

        public string source
        {
            get
            {
                return this._source;
            }
            set
            {
                base.IsDirty = value != this._source;
                this._source = value;
            }
        }

        public string sourcetype
        {
            get
            {
                return this._sourcetype;
            }
            set
            {
                base.IsDirty = value != this._sourcetype;
                this._sourcetype = value;
            }
        }
    }
}

